from pub.routine import TcpClient
# from pub.routine import UdpClient
from broker.routine import UdpImgClient as UdpClient
from broker.protocal import RequestForwardTrans, BROKER_PORT

BROKER_ADDR = "127.0.0.1"

if __name__ == "__main__":
    client = UdpClient((BROKER_ADDR, BROKER_PORT))
    protocal = RequestForwardTrans()
    client.set_protocal(protocal)
    client.listen(runHeart=True)

    protocal.register_assist("123456")
    # client.setDaemon(True)

    try:
        while True:
            data = input("Input the Data ([Q] for quit): ")
            if data == "Q":
                protocal.connect_close()
                client.stop()
                break
            else:
                try:
                    # 可直接输入“1024”，则输出 “1024xxxx...”
                    num = int(data)
                    data += "x"*(num - len(data))
                    print(f"发送数据共计【{num}】bytes")
                except:
                    # 作为普通字符串发送
                    pass

                protocal.send_data(data.encode())
    except OSError:
        print("由于心跳超时，已自动断开连接")

    client.stop()
    print("End")
